package com.flyone.fuxiao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.flyone.fuxiao.mapper.PostHistoryMapper;
import com.flyone.fuxiao.model.entity.PostHistory;
import com.flyone.fuxiao.model.entity.User;
import com.flyone.fuxiao.model.vo.DrawHistoryRecordVO;
import com.flyone.fuxiao.model.vo.PostHistoryVO;
import com.flyone.fuxiao.service.PostHistoryService;
import com.flyone.fuxiao.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* @author luoyifei
* @description 针对表【post_history(历史记录表)】的数据库操作Service实现
* @createDate 2024-03-28 21:12:49
*/
@Service
public class PostHistoryServiceImpl extends ServiceImpl<PostHistoryMapper, PostHistory>
    implements PostHistoryService{

    @Resource
    private UserService userService;

    @Override
    public List<PostHistoryVO> getAllHistory(HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        QueryWrapper<PostHistory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId",loginUser.getId());
        List<PostHistory> postHistoryList = list(queryWrapper);
        // 去除token重复
        Map<String, PostHistoryVO> uniquePostHistoryMap = postHistoryList.stream()
                .map(PostHistoryVO::new) // 转换为 PostHistoryVO 类型
                .collect(Collectors.toMap(
                        // 键选择器，提取 token 字段作为键
                        PostHistoryVO::getToken,
                        // 值选择器，保持不变，即 PostHistoryVO 对象本身
                        Function.identity(),
                        // 当键重复时，保留最新出现的那个对象
                        (existingValue, newValue) -> existingValue));
        return new ArrayList<>(uniquePostHistoryMap.values());
    }

    @Override
    public List<DrawHistoryRecordVO> getHistoryRecordByToken(String token, HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        QueryWrapper<PostHistory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId",loginUser.getId());
        queryWrapper.eq("assignUUID",token);
        List<PostHistory> postHistoryList = list(queryWrapper);
        return postHistoryList.stream().map(postHistory -> {
            DrawHistoryRecordVO historyRecordVO = new DrawHistoryRecordVO();
            String contentUrl = postHistory.getContentUrl();
            if (contentUrl != null) {
                contentUrl = contentUrl.replace("[", "").replace("]", "");
                String[] split = contentUrl.split(",");
                historyRecordVO.setImages(new ArrayList<>());
                for (String s : split) {
                    historyRecordVO.getImages().add(s);
                }
            }
            historyRecordVO.setPrompt(postHistory.getPrompt());
            return historyRecordVO;
        }).collect(Collectors.toList());
    }

    @Override
    public String deleteHistoryRecordByToken(String token, HttpServletRequest request) {
        boolean assignUUID = this.remove(new QueryWrapper<PostHistory>().eq("assignUUID", token));
        return assignUUID ? "success" : "fail";
    }
}




